const cloud = require('wx-server-sdk');

const db = cloud.database()

exports.main = async (event,context)=>{
	let query = db.collection('follower').where({
		user:event.data.user,
		follower:event.data.follower
	})
	let counts = await query.count();
	let infos;
	if(counts.total === 0){
		if(event.method == 'post'){
			infos = await db.collection('follower').add({
				data: event.data
			})
		}else{
			infos = {
				isFollower:false
			}
		}
	}else{
		if(event.method == 'post'){
			infos = await query.update({
				data:{
					isfollower:event.data.isfollower
				}
			})
		}else{
			let result = await query.limit(1).get();
			console.log(result);
			infos = {
				isFollower:result.data[0].isfollower
			}
		}
	}
	return {'msg':'ok','data':infos}
}